﻿@using Sharp.DataTransferObject
@using Sharp.Infrastructure.WebApiClient
@using Sharp.ServiceContracts
@using Sharp.WebBooter.Areas.Platform.Controllers
@{
    List<OrgIndexViewModel> newOrgList = new List<OrgIndexViewModel>();
    IEnumerable<OrgDTO> list;

    using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
    {
        list = proxy.Channel.GetOrgs().Result;
    }

    OrgController.Build(list, newOrgList, null);
}
<script>
    $(document).ready(function () {
        var t = $('#employeeList').dataTable({
            "serverSide": true,
            displayLength: 8,
            bInfo: false,
            dom:
                "<'row'<'col-sm-6'l><'col-sm-6'f>>" +
                    "<'row'<'col-sm-12'tr>>" +
                    "<'row'<'col-sm-12'p>>",
            "ajax": {
                "url": "@Url.Action("GetPaged", "Employee")",
                "type": "POST"
            },
            select: { style: 'single', selector: 'td:first-child'},
            "columnDefs": [{ sDefaultContent: '', aTargets: ['_all'] }],
            columns: [
                { title: '', data: null, sortable: false, width: 30, "sClass": 'checkColumn select-checkbox' },
                { title: '工号', data: 'code', width: 70, "sClass": "center" },
                { title: "姓名", data: 'fullName', width: 160, "sClass": "left" },
                {
                    title: "性别", data: 'genderCode', render: function (data, type, row, meta) {
                        var genderCode;
                        if (row.person) genderCode = row.genderCode;
                        else genderCode = "";
                        return app.code2Text("Gender", genderCode);
                    },
                    "sClass": "left"
                }
            ]
        });
    });
    var selectBoxViewModel = function () {
        var self = this;
        self.selectThis = function (datalistControl, event) {

            var data = $("#" + datalistControl).DataTable().rows({ selected: true }).data();
            if (data.length === 0) {
                window.app.message("请选中一项。");
                event.preventDefault();
                event.stopPropagation();
                return;
            }
            var row = data[0];
            var $selectTextControl = $("#@ViewBag.SelectTextControl");
            var $triggerControl = $("#" + $selectTextControl.attr("id") + "Trigger");
            var valueField = $triggerControl.data("valuefield");

            $selectTextControl.val(row.fullName);
            $("#" + valueField).val(row.id);


            $selectTextControl.change();
            $selectTextControl.valid();
        }
        self.empty = function (datalistControl) {
             var $selectTextControl = $("#@ViewBag.SelectTextControl");
            var $triggerControl = $("#" + $selectTextControl.attr("id") + "Trigger");
            var valueField = $triggerControl.data("valuefield");

            $selectTextControl.val("");
            $("#" + valueField).val("");


            $selectTextControl.change();
            $selectTextControl.valid();
        }
    }


</script>
<div class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">选取员工</h4>
            </div>
            <div class="modal-body no-pad-top">
                <script>
                    function orgChange(obj, orgId) {
                        var $Searchform = $(obj).parents(".modal-body").find("form");

                        var orgIds = new Array();

                        var $c = $Searchform.find("input[name='OrgId']");
                        $c.val("");
                        $Searchform.find("input[clone='1']").remove();
                        $("#orgTreeDataList .checkColumn input[name='cellCheckbox']:checked").each(function (index, item) {
                            if (item.checked) {
                                if (index === 0) {
                                    $c.val(item.value);
                                } else {

                                    $Searchform.append($("<input name='OrgId' type='hidden' clone='1' />").val(item.value));
                                }
                                orgIds.push(item.value);
                            }
                        });

                        $Searchform.find(".btn").click();
                    }
                </script>

                <div class="row">
                    <div class="col-md-4">
                        <div class="box box-solid">
                            <div class="box-header with-border">
                                <h3 class="box-title">组织</h3>

                                <div class="box-tools">
                                    <button type="button" class="btn btn-box-tool" data-widget="collapse">
                                        <i class="fa fa-minus"></i>

                                    </button>
                                </div>
                            </div>
                            <div class="box-body no-padding" style="height: 400px;overflow-y:scroll">

                                <table id="orgTreeDataList" class="table table-bordered table-striped table-condensed table-hover">
                                    <thead>
                                        <tr>
                                            <th style="width:43px;" class="center"></th>
                                            <th style="width:240px;">名称</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @{
                                            foreach (var item in newOrgList)
                                            {
                                                <tr class="@item.Itemclass">
                                                    <td style="width:43px;" class="center checkColumn"><label><input type="checkbox" name="cellCheckbox" value="@item.Id" onclick="orgChange(this, '@item.Id')" /><i></i></label></td>
                                                    <td>
                                                        @item.Title
                                                    </td>
                                                </tr>
                                            }
                                        }
                                    </tbody>
                                </table>
                                <script>
                                    $('#orgTreeDataList').treegrid({
                                        treeColumn: 1,
                                        expandAll: false  //是否全部展开
                                    });
                                </script>

                            </div>
                        </div>

                    </div>
                    <!-- /.col -->
                    <div class="col-md-8">
                        <div class="pull-right text-black">
                            <form class="input-group input-group-sm SearchArea">
                                <input type="text" name="fullName" autocomplete="off" class="form-control pull-right" placeholder="输入姓名查询">
                                <div class="input-group-btn">
                                    <button type="button" class="btn btn-default" onclick="window.app.search(this);" data-search="#employeeList"><i class="fa fa-search"></i></button>
                                </div>
                                <input name="OrgId" type="hidden" />
                            </form>
                        </div>


                        <table id="employeeList" class="table table-bordered table-striped" width="100%"></table>
                    </div>
                </div>


            </div>
            <div class="modal-footer no-pad-top">
                <div class="btn-group">
                    @Html.Bootstrap().LinkButton("确定", ToolBarIco.Save).Style(BootstrapStyle.Primary).Click("new selectBoxViewModel().selectThis('employeeList',event)").Attribute("data-dismiss", "modal")
                    @*@Html.Bootstrap().LinkButton("清除", ToolBarIco.Permission).Style(BootstrapStyle.Warning).Click("new selectBoxViewModel().empty('employeeList')").Attribute("data-dismiss", "modal")*@
                    @Html.Bootstrap().LinkButton("关闭", ToolBarIco.Close).Style(BootstrapStyle.Default).Attribute("data-dismiss", "modal")
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>